#include "hcthread.h"
#include "qmetaobject.h"

 ak_hash *make_hash_ctx( std::string alg )
{
  ak_hash *hctx = NULL;

  try {
   return( hctx = ak_make_hash( alg ));
  } catch( ak_error ) {  }
  return NULL;
}

HCThread::HCThread(MainWindow* parent, QStringList& files, QMap<QString, HashLine *>& hashes):
		ProgressThread(files, parent),
		_hashes(hashes)
{
}

bool HCThread::job(QString &fileName) {

	ak_master< ak_hash > hctx = NULL;
	ak_master< ak_retcode > ret = NULL;
   
	if( hctx == NULL ) {
		if( (hctx = make_hash_ctx( _hashes[fileName]->alg().toStdString() )) == NULL )
			return true;
    } else {
		if( ak_oids_by_oid( hctx->oid()).name() != _hashes[fileName]->alg().toStdString() ) {
			hctx.free();
			if( (hctx = make_hash_ctx( _hashes[fileName]->alg().toStdString() )) == NULL )
				return true;
		}
	}
        
    try {
		ret = hctx->hash_file( fileName.toStdString() );
		CheckResult cr(fileName, _hashes[fileName]->hash().toStdString() == ak_tostring( ret ));
		_out.append(cr);
	} catch( ak_error e ) { }

	
	return true;
}

HCThread::~HCThread() {
	
}

CheckResult::CheckResult(QString filename, bool status):
						_filename(filename), _status(status){

}

void CheckResult::setFilename(QString file) {
	_filename=file;
}

void CheckResult::setStatus(bool stat) {
	_status=stat;
}

QString CheckResult::filename() const {
	return _filename;
}

bool CheckResult::status() const {
	return _status;
}
